﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Squog_1._1.Managers;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Squog_1._1.Menu
{
    //EventHandeler for selection of a button
    public delegate void ButtonSelectedEvent(object o, SelectedEventArgs e);
    public delegate void ButtonHighlightedEvent(object o, HighlightedEventArgs e);

    //Arguments for the event handelers
    public class SelectedEventArgs : EventArgs
    {
        public readonly int SelectionNumber;

        public SelectedEventArgs(int SelectionNumber)
        {
            this.SelectionNumber = SelectionNumber;
        }
    }
    public class HighlightedEventArgs : EventArgs
    {
        public readonly int SelectionNumber;

        public HighlightedEventArgs(int SelectionNumber)
        {
            this.SelectionNumber = SelectionNumber;
        }
    }


    /// <summary>
    /// Button The appears in a window, Intedened to be used within the ButtonLayout class.
    /// </summary>
    public class WindowButton
    {
        public String Text;
        public bool Selectable;
        public SpriteFont Font;
        private Point size;
        private Rectangle buttonRect;
        private Vector2 position;
        public Point Size
        {
            get { return size; }
            set
            {
                size = value;
                buttonRect.Width = size.X;
                buttonRect.Height = size.Y;
            }
        }
        public Rectangle ButtonRect
        {
            get { return buttonRect; }
        }
        public Vector2 Position
        {
            get { return position; }
            set
            {
                position = value;
                buttonRect.X = (int)value.X - 10;
                buttonRect.Y = (int)value.Y;
            }
        }

        public WindowButton(String Text, SpriteFont Font, bool Selectable)
            : this(Text, Font, Selectable, new Point((int)Font.MeasureString(Text).X + 20, (int)Font.MeasureString(Text).Y))
        { }

        public WindowButton(String Text, SpriteFont Font, bool Selectable, Point Size)
        {
            buttonRect = new Rectangle(0, 0, 0, 0);
            this.Text = Text;
            this.Selectable = Selectable;
            this.Size = Size;
            this.Position = new Vector2(0, 0);
            this.Font = Font;
        }
    }

    class ButtonLayout
    {
        //EventHandelers
        public event ButtonSelectedEvent SelectedDel;
        public event ButtonHighlightedEvent HighlightedDel;

        private List<WindowButton> LayoutButtons;
        private Vector2 Position;

        private static Texture2D ButtonBackground;
        private static Texture2D SelectedBackground;

        private int Selected = 0;

        public static void LoadTextures(ContentManager Content)
        {
            ButtonBackground = Content.Load<Texture2D>(@"Menu\ButtonBG");
            SelectedBackground = Content.Load<Texture2D>(@"Menu\SelectedButtonBG");
        }

        public ButtonLayout(Vector2 InitialPosition)
        {
            LayoutButtons = new List<WindowButton>();
            Position = InitialPosition;
        }

        public void AddButtonToEnd(string Text, SpriteFont Font, bool IsSelectable)
        {
            LayoutButtons.Add(new WindowButton(Text, Font, IsSelectable));

            Vector2 NextButtonPosition = Position;

            for (int i = 0; i < LayoutButtons.Count(); i++)
            {
                NextButtonPosition = Position;
                NextButtonPosition.Y += (LayoutButtons[i].Size.Y) * i;
                LayoutButtons[i].Position = NextButtonPosition;            
            }

            int Max = 0;
            foreach (WindowButton B in LayoutButtons)
                if (B.Size.X > Max)
                    Max = B.Size.X;
            for (int i = 0; i < LayoutButtons.Count(); i++)
                LayoutButtons[i].Size = new Point(Max, LayoutButtons[i].Size.Y);

        }
        public void AddButtonToEnd(WindowButton NewButton)
        {
            LayoutButtons.Add(NewButton);

            for (int i = 0; i < LayoutButtons.Count(); i++)
            {
                LayoutButtons[i].Position = Position;
                Position.Y += LayoutButtons[i].Size.Y;
            }

            int Max = 0;
            foreach (WindowButton B in LayoutButtons)
                if (B.Size.X > Max)
                    Max = B.Size.X;
            for (int i = 0; i < LayoutButtons.Count(); i++)
                LayoutButtons[i].Size = new Point(Max, LayoutButtons[i].Size.Y);

        }

        public void Update()
        {
            if (InputManager.OnPress(Thumbsticks.Left, 0, Direction.Up) ||
                InputManager.OnPress(Keys.W, 0) ||
                InputManager.OnPress(Keys.Up, 0))
            {
                Selected--;
                Selected += LayoutButtons.Count;
                Selected %= LayoutButtons.Count;
                if (HighlightedDel != null)
                    HighlightedDel(this, new HighlightedEventArgs(Selected));
            }

            if (InputManager.OnPress(Thumbsticks.Left, 0, Direction.Down) ||
                InputManager.OnPress(Keys.S, 0) ||
                InputManager.OnPress(Keys.Down, 0))
            {
                Selected++;
                Selected += LayoutButtons.Count;
                Selected %= LayoutButtons.Count;
                if (HighlightedDel != null)
                    HighlightedDel(this, new HighlightedEventArgs(Selected));
            }

            if(InputManager.OnPress(Buttons.A, 0) ||
               InputManager.OnPress(Buttons.Start, 0) ||
               InputManager.OnPress(Keys.Enter, 0))
            {
                if(SelectedDel != null)
                    SelectedDel(this, new SelectedEventArgs(Selected));
            }
        }

        public void DrawButtons(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < LayoutButtons.Count(); i++)
            {
                if (ButtonBackground == null || SelectedBackground == null)
                    throw new Exception("LoadTextures() was not called before DrawButtons() in ButtonLayout");

                //Draws the background background
                if (ButtonBackground != null)
                    spriteBatch.Draw(ButtonBackground, LayoutButtons[i].ButtonRect, Color.White);

                //Handles the selected Button
                if (SelectedBackground != null && Selected == i)
                    spriteBatch.Draw(SelectedBackground, LayoutButtons[i].ButtonRect, Color.White);

                //Writes the text on the button
                if (LayoutButtons[i].Selectable)
                    spriteBatch.DrawString(LayoutButtons[i].Font, LayoutButtons[i].Text, LayoutButtons[i].Position, Color.DarkOrange);
                else
                    spriteBatch.DrawString(LayoutButtons[i].Font, LayoutButtons[i].Text, LayoutButtons[i].Position, Color.DarkRed);
            }
        }
    }
}
